//
//  WKBeautysayDetailView.m
//  WKMaybell
//
//  Created by 何文虎 on 2018/12/21.
//  Copyright © 2018 何文虎. All rights reserved.
//

#import "WKBeautysayDetailView.h"
#import "WKContenCellPhotoView.h"

@interface WKBeautysayDetailView ()<SDPhotoBrowserDelegate>
@property(nonatomic,strong)UIImageView *headerImgView;
@property(nonatomic,strong)UILabel *nameLb;
@property(nonatomic,strong)UILabel *timeLb;
@property(nonatomic,strong)UILabel *contentLb;
@property(nonatomic,strong)UIView *contentView;
@property(nonatomic,strong)UILabel *readNumLb;

@property(nonatomic,strong)NSArray *imgArr;
@end

@implementation WKBeautysayDetailView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

- (void)setup{
    
    CGFloat margin = 12;
    [self addSubview:self.headerImgView];
    [self.headerImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(margin);
        make.top.mas_equalTo(margin);
        make.size.mas_equalTo(CGSizeMake(36, 36));
    }];
    
    [self addSubview:self.nameLb];
    [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_headerImgView.mas_right).offset(margin);
        make.top.mas_equalTo(_headerImgView.mas_top);
        make.size.mas_equalTo(CGSizeMake(80, 14));
    }];
    
    
    [self addSubview:self.timeLb];
    [self.timeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_nameLb.mas_left);
        make.top.mas_equalTo(_nameLb.mas_bottom).offset(8);
        make.size.mas_equalTo(CGSizeMake(200, 14));
    }];
    
    [self addSubview:self.contentLb];
    [self.contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(margin);
        make.right.mas_equalTo(-margin);
        make.top.mas_equalTo(_headerImgView.mas_bottom).offset(10);
        make.size.mas_equalTo(CGSizeMake(200, 14));
    }];
    
    [self addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(margin);
        make.top.mas_equalTo(_contentLb.mas_bottom).offset(10);
        make.right.mas_equalTo(-margin);
        
    }];
    
    [self addSubview:self.readNumLb];
    [self.readNumLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(margin);
        make.top.mas_equalTo(_contentView.mas_bottom).offset(10);
        make.right.mas_equalTo(-margin);
        make.height.mas_equalTo(15);
    }];

    _headerImgView.layer.masksToBounds = YES;
    _headerImgView.layer.cornerRadius = 36/2;
    _headerImgView.contentMode = UIViewContentModeScaleAspectFill;
    
   
    _contentLb.numberOfLines = 3;
    _contentLb.preferredMaxLayoutWidth = ScreenWidth - 24;
    [_contentLb sizeToFit];
    
    [_headerImgView sd_setImageWithURL:[NSURL URLWithString:@"http://img1.touxiang.cn/uploads/20120731/31-060351_329.jpg"]];
    _nameLb.text = @"会飞翔的鱼";
    _timeLb.text = @"9-17 12:30";
     _readNumLb.text = @"浏览量15w+";
    NSString *content = @"想到了儿时的邻居们，傍晚用过晚膳后，在门前的空地上，大人们或站或坐，儿童们嬉戏打闹，大人们谈时事政治，谈金融危机，谈世界风 云，谈体育赛事，谈儿童教育，谈奇闻异事;儿童们捉迷藏，跳方格，踢毽子。未了，儿童们乏了，大人们的新闻也枯竭了，就开始指着天 空告诉我们，那个像勺子的叫北斗七星，那个对着北斗七星最亮的叫北极星，天上还有牛郎星与织女星。";
   
    _contentLb.text = content;
    CGFloat contentHeight = [content boundingRectWithSize:CGSizeMake(ScreenWidth-24, MAXFLOAT) font:PFSC_RegularFont(15) lineSpacing:5].height;
    CGFloat maxcontentHeight = 10+_contentLb.font.lineHeight * 3;
    contentHeight = contentHeight > maxcontentHeight ? maxcontentHeight : contentHeight;
    
    
    [_contentLb mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(contentHeight);
    }];

    self.userInteractionEnabled = YES;
    
    
    WKContenCellPhotoView *photoView = [WKContenCellPhotoView new];
    photoView.userInteractionEnabled = YES;
    [_contentView addSubview:photoView];
    [photoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.width.mas_equalTo(0);
    }];
    
    
    self.imgArr =  @[@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1545396005838&di=b79f2e55e54c600a69bb20615a4b7a5c&imgtype=0&src=http%3A%2F%2Fimg.vip.xunlei.com%2F2010-february-wallpaper%2Fsweet%2Fb%2F1280-800.jpg",@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1545396005838&di=1eed1af7918fd1c7c9648e582614b490&imgtype=0&src=http%3A%2F%2Fimg.bimg.126.net%2Fphoto%2Fq-i-sQFAewKeaeoRzjeVXw%3D%3D%2F3398247393828181060.jpg",@"https://timgsa.baidu.com/timg?image&quality=80&size=b10000_10000&sec=1545385956&di=e29e1dba154c930cf3e41a4534361df1&src=http://scimg.jb51.net/allimg/161107/102-16110G14553921.jpg",@"https://timgsa.baidu.com/timg?image&quality=80&size=b10000_10000&sec=1545385971&di=a217208be977525780112255670187d5&src=http://img.zcool.cn/community/010a2756a81db86ac7256cb09965d4.png"];
    photoView.picPathStringsArray = self.imgArr;
    CGFloat height = photoView.height;
    
    [_contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(height);
    }];
    
}


#pragma mark点击图片
- (void)tapImageView:(UITapGestureRecognizer *)tap
{
    UIView *imageView = tap.view;
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    browser.currentImageIndex = imageView.tag;
    browser.sourceImagesContainerView = self;
    browser.imageCount = self.imgArr.count;
    browser.delegate = self;
    [browser show];
}


#pragma mark - SDPhotoBrowserDelegate

- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{
    NSString *imageName = self.imgArr[index];
    NSURL *url = [[NSBundle mainBundle] URLForResource:imageName withExtension:nil];
    return url;
}

- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    UIImageView *imageView = self.subviews[index];
    return imageView.image;
}


- (UIImageView*)headerImgView{
    if (!_headerImgView) {
        _headerImgView = [[UIImageView alloc]init];
    }
    return _headerImgView;
}

- (UILabel*)nameLb{
    if (!_nameLb) {
        _nameLb = [[UILabel alloc]init];
        _nameLb.textColor = UIColorFromHex(0x3C3C3C);
        _nameLb.font = PFSC_MediumFont(15);
    }
    return _nameLb;
}

- (UILabel*)timeLb{
    if (!_timeLb) {
        _timeLb = [[UILabel alloc]init];
        _timeLb.textColor = UIColorFromHex(0x979797);
        _timeLb.font = PFSC_MediumFont(11);
    }
    return _timeLb;
}

- (UILabel*)contentLb{
    if (!_contentLb) {
        _contentLb = [[UILabel alloc]init];
        _contentLb.textColor = UIColorFromHex(0x3C3C3C);
        _contentLb.font = PFSC_RegularFont(15);
    }
    return _contentLb;
}

- (UIView*)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc]init];
    }
    return _contentView;
}

- (UILabel*)readNumLb{
    if (!_readNumLb) {
        _readNumLb = [[UILabel alloc]init];
        _readNumLb.textColor = UIColorFromHex(0x979797);
        _readNumLb.font = PFSC_MediumFont(10);
    }
    return _readNumLb;
}

@end
